package io.lkt.task.task.process.articlePush;

import io.lkt.task.bus.mapper.log.PushDetailLogMapper;
import io.lkt.task.bus.mapper.log.PushHeadLogMapper;
import io.lkt.task.bus.model.UserInfo;
import io.lkt.task.bus.model.log.PushHeadLog;
import io.lkt.task.bus.service.IUserBrandService;
import io.lkt.task.bus.service.IUserService;
import io.lkt.task.task.async.UploadTempMsgPushAsyncTask;
import io.lkt.task.wechat.service.WeixinServiceDL;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class UploadTempMsgPushTask extends QuartzJobBean {

    @Autowired
    private UploadTempMsgPushAsyncTask uploadTempMsgPushAsyncTask;

    @Autowired
    private IUserService userService;

    @Value("${sleepTime}")
    private String sleepTimeSet;

    @Autowired
    private PushHeadLogMapper pushHeadLogMapper;

    @Autowired
    private PushDetailLogMapper pushDetailLogMapper;

    @Autowired
    private ObjectFactory<WeixinServiceDL> objectFactory;

    @Value("${gzhCodes}")
    private String springProfilesActive;

    @Autowired
    private IUserBrandService userBrandService;

    @SneakyThrows
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        String profiles_str = springProfilesActive;//获取环境
        String[] profile_arr = profiles_str.split(",");
        List<Integer> headIds = new ArrayList<>();

        LocalDate now = LocalDate.now();
        Integer week = now.getDayOfWeek().getValue();

        for (String profile : profile_arr) {

            Integer headId = -1;
            headId = uploadTempMsgPushAsyncTask_Execute(profile);
            if (!headId.equals(-1)) {
                headIds.add(headId);
            }
        }

        if (headIds != null && headIds.size() > 0) {
            Thread.sleep(1000 * 60 * 30);
            for (Integer headId : headIds) {
                PushHeadLog pushHeadLogU = new PushHeadLog();
                pushHeadLogU.setId(headId);
                pushHeadLogU.setSuccessCount(pushDetailLogMapper.getSuccessCount(headId));
                pushHeadLogU.setErrCount(pushDetailLogMapper.getErrCount(headId));
                pushHeadLogU.setUpdateTime(new Date());
                pushHeadLogU.setEndTime(new Date());

                pushHeadLogMapper.updatePushHeadLog(pushHeadLogU);
            }
        }
    }

    private Integer uploadTempMsgPushAsyncTask_Execute(String profile) {
        PushHeadLog pushHeadLog = new PushHeadLog();
        pushHeadLog.setPushName("品牌文章上架推送");
        pushHeadLog.setMsgType(14);
        pushHeadLog.setMsgTypeName("品牌文章上架推送");
        pushHeadLog.setSumCount(0);
        pushHeadLog.setCreateTime(new Date());
        pushHeadLog.setGzhCode(profile);

        pushHeadLogMapper.addPushHeadLog(pushHeadLog);
        Integer headId = pushHeadLog.getId();

        List<UserInfo> userList = userService.getUserListByNoActivation(profile);
        for (UserInfo userInfo : userList) {
            uploadTempMsgPushAsyncTask.execute(userInfo, profile, headId);
        }

        return headId;
    }
}
